<?php

  require_once(ROOT. "lib/DB/MysqlDB.php");

class Table{
  var $db;
  var $table_name;

  function __construct($table_name){
    $this -> db = new MysqlDB(array(
      "dbHost"=>"hpx.hpx1011.unibeing.net",
      "dbName"=>"hpx",
      "dbUser"=>"hpx",
      "dbPswd"=>"rannninngu",
      "charset"=>"UTF-8"
    ));
    $this -> table_name = $table_name;
  }
  function get_where_condition($where){
    if (isset ($where)){
      $sql = " where ";
      foreach ($where as $i => $j){
        if (preg_match('/\?/', $i)){
          $sql = $sql . " $i and ";
        }else{
          $sql = $sql . " $i=? and ";
        }
      }

      $sql = $sql . " 1=1 ";
      return $sql;
    }else{
      return "";
    }
  }  
  function get_fields(){
    return $this->db->field_names();
  }

  function change_table($table_name){
    $this -> table_name = $table_name;
  }
  function get_table_name(){
    return $this -> table_name;
  }
  function create($hash){
    $keys = array ();
    $values = array ();
    $placehold = array ();

    foreach ((array)$hash as $i => $j){
      if ($i != "id" && $j){
        $keys[] = "`$i`";
        $values[] = $j;
        $placehold[] = "?";
      }
    }

    $this -> bind_exec("insert ignore into " . $this -> get_table_name() . "(" . join(",", $keys) . ") values(" . join(",", $placehold) . ")", $values);
    return true;
  }

  function save($hash){
    if ($hash["id"]){
      return $this -> update($hash);
    }else{
      return $this -> create($hash);
    }
  }

  function update($hash){
    $keys = array ();
    $values = array ();
    $placehold = array ();

    foreach ($hash as $i => $j){
      if ($i == "id"){
        continue;
      }
      $keys[] = " `" . $i . "`=?";
      $values[] = $j;
    }

    $where = " where id = ?";
    $values[] = $hash["id"];

    $this ->bind_exec("update " . $this -> get_table_name() . " set " . join(",", $keys) . " " . $where, $values);
    return true;
  }

  function bind_exec($sql,$values){
    $this -> db -> update($sql, $values);
    return true;
  }
  function exec($sql){
    return $this->bind_exec($sql,null);
  }
  function peek_update($hash, $f){
    $values = array ();
    $sql = "update " . $this -> get_table_name() . "  set ";
    $keys = array ();
    foreach ($hash as $i => $j){
      if ($i == "id"){
        next;
      }
      $keys[] = " " . $i . "=?";
      $values[] = $j;
    }
    $sql .= join(",", $keys) . " ";
    
    $sql .= $this->get_where_condition($f);

    if (isset ($f)){
      foreach ($f as $i => $j){
          $values[] = $j;
      }
    }

    $this -> bind_exec($sql, $values);
    return true;
  }



  function all($related = false){
    return $this -> peeks(array (), $related);
  }
  function del_by_id($id){
    return $this -> peek_del(array ("id" => $id
        ));
  }
  function find_by_id($id, $related = false){
    return $this -> peek(array ("id" => $id
        ), $related);
  }
  function peek($f){
    $all = $this -> peeks($f);
    if ($all && count($all) >= 1){
      return $all[0];
    }
    return null;
  }

  function peeks($f=null){
    $values = array ();
    $sql = "select * from " . $this -> get_table_name() . "  ";
    
    $sql .= $this->get_where_condition($f);
    
    if (isset ($f)){
      foreach ($f as $i => $j){
        $values[] = $j;
      }
    }
    
    $all=$this -> bind_select($sql, $values);
    
    if ($all && count($all) >= 0){
      return $all;
    }
    return null;
  }
  
  function peek_del($f){
    $values = array ();
    $sql = "delete from " . $this -> get_table_name() . "  ";
    
    $sql .= $this->get_where_condition($f);
    
    if (isset ($f)){
      foreach ($f as $i => $j){
        $values[] = $j;
      }
    }
    
    $all=$this -> bind_exec($sql, $values);

    return true;
  }

  function bind_select($sql,$params){
    $all = $this -> db -> getAll($sql,$params);
    if ($all && count($all) >= 0){
      return $all;
    }
    return null;
  }
  function bind_select_where($sql,$params=null){
    $sql_new = "select * from " . $this -> get_table_name() . "  $sql";
    return $this->bind_select($sql_new,$params);
  }
  function count($sql="",$params=null){
    $all = $this -> bind_select("select count(*) as records_num_model from ". $this -> get_table_name() . "  $sql",$params);
    if ($all && count($all) >= 1){
      return $all[0]["records_num_model"];
    }
    return null;
  }


}

?>